﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using WeLinkApi.Common;
using WeLinkApi.Models;
using WeLinkApi.Models.Request;
using Newtonsoft.Json;

namespace WeLinkApi
{
    public class WeLinkApiAction
    {
        //API版本
        private static readonly string WelinkUrl;
        static WeLinkApiAction()
        {
            WelinkUrl = ConfigurationManager.AppSettings["WeLinkUrl"];
        }

        private static Encoding DafaultEncoding = Encoding.UTF8;

        private string meetingsUrl;

        public WeLinkApiAction(string url)
        {
            meetingsUrl = ConfigurationManager.AppSettings["WelinkMeetingUrl"];
        }


        #region ExecuteAction_Meeting
        public T Execute<T>(ApiRequest request) where T : ApiResponse
        {
            var jsonResult = ExecuteGetJsonStr(request);
            return JsonConvert.DeserializeObject<T>(jsonResult);
        }

        public T ExecuteAction<T>(ApiRequest request, string token) where T : ApiResponse
        {
            var jsonResult = ExecuteGetJsonStrByToken(request, token);

            return JsonConvert.DeserializeObject<T>(jsonResult.Substring(1, jsonResult.Length - 2));//去除最外面的[]
        }

        public T ExecuteActionUnil<T>(ApiRequest request, string token) where T : ApiResponse
        {
            var jsonResult = ExecuteGetJsonStrByToken(request, token);

            return JsonConvert.DeserializeObject<T>(jsonResult);
        }

        public T ExecuteActionNoReq<T>(ApiRequest request, string token) where T : ApiResponse
        {
            var jsonResult = ExecuteSendGetRequest(request, token);

            return JsonConvert.DeserializeObject<T>(jsonResult);
        }

        public T ExecuteDeleteActionNoReq<T>(ApiRequest request, string token) where T : ApiResponse
        {
            var jsonResult = ExecuteSendDeleteRequest(request, token);

            return JsonConvert.DeserializeObject<T>(jsonResult);
        }
        public T ExecutePutActionNoReq<T>(ApiRequest request, string token) where T : ApiResponse
        {
            var jsonResult = ExecuteSendPutRequest(request, token);

            return JsonConvert.DeserializeObject<T>(jsonResult.Substring(1, jsonResult.Length - 2));
        }
        #endregion

        #region ExcuteAction_WeLink

        public T ExecuteUnit<T>(ApiRequest request) where T : ApiResponse
        {
            var jsonResult = ExecuteGetJsonStrWeLink(request);
            return JsonConvert.DeserializeObject<T>(jsonResult);
        }

        public T ExecuteUnitND<T>(ApiRequest request,string token ) where T : ApiResponse
        {
            var jsonResult = ExecuteSendTokenRequestWeLink(request, token);
            return JsonConvert.DeserializeObject<T>(jsonResult);
        }
        public T ExecuteUnitGet<T>(ApiRequest request, string token) where T : ApiResponse
        {
            var jsonResult = ExecuteSendGetTokenRequestWeLink(request, token); 
            return JsonConvert.DeserializeObject<T>(jsonResult);
        }

        public T ExecuteActionAsyn<T>(ApiRequest request, string token) where T : ApiResponse
        {
            var jsonResult = ExecuteSendAsynRequestWeLink(request, token);

            return JsonConvert.DeserializeObject<T>(jsonResult);
        }
        #endregion

        #region ExceteJson
        public string ExecuteGetJsonStr(ApiRequest request)
        {
            var url = GetRequestUrl(request);
            var jsonData = GetRequestParamJsonStr(request);

            return HttpWebRequestHelper.PostJsonToUrl(url, jsonData);
        }
        public string ExecuteGetJsonStrByToken(ApiRequest request, string token)
        {
            var url = GetRequestUrl(request);
            var jsonData = GetRequestParamJsonStr(request);
            return HttpWebRequestHelper.PostJsonToUrlByToken(url, jsonData, token);
        }

        public string ExecuteSendGetRequest(ApiRequest request, string token)
        {
            var url = GetRequestUrl(request);
            return HttpWebRequestHelper.SendGetRequest(url, token);
        }

        public string ExecuteSendDeleteRequest(ApiRequest request, string token)
        {
            var url = GetRequestUrl(request);
            return HttpWebRequestHelper.SendDeleteRequest(url, token);
        }

        public string ExecuteSendPutRequest(ApiRequest request, string token)
        {
            var url = GetRequestUrl(request);
            var jsonData = GetRequestParamJsonStr(request);
            return HttpWebRequestHelper.PutJsonToUrlByToken(url, jsonData, token);
        }


        public string ExecuteGetJsonStrWeLink(ApiRequest request)
        {
            var url = GetWeLinkRequestUrl(request);
            var jsonData = GetRequestParamJsonStr(request);
            return HttpWebRequestHelper.PostJsonToUrlWeLink(url, jsonData);
        }

        public string ExecuteSendTokenRequestWeLink(ApiRequest request, string token)
        {
            var url = GetWeLinkRequestUrl(request);
            return HttpWebRequestHelper.PostUrlWeLinkNoJson(url, token);
        }

        public string ExecuteSendGetTokenRequestWeLink(ApiRequest request, string token)
        {
            var url = GetWeLinkRequestUrl(request);
            return HttpWebRequestHelper.SendGetToWeLinkRequest(url, token);
        }

        public string ExecuteSendAsynRequestWeLink(ApiRequest request, string token)
        {
            var url = GetWeLinkRequestUrl(request);
            var jsonData = GetRequestParamJsonStr(request);
            return HttpWebRequestHelper.PostJsonToUrlByTokenWeLink(url, jsonData, token);
        }
        #endregion

        #region GetUrl

        private string GetRequestUrl(ApiRequest request)
        {
            StringBuilder urlSb = new StringBuilder(meetingsUrl);
            urlSb.Append(request.GetApiMethod());
            return urlSb.ToString();
        }

        private string GetWeLinkRequestUrl(ApiRequest request)
        {
            StringBuilder urlSb = new StringBuilder(WelinkUrl);
            urlSb.Append(request.GetApiMethod());
            return urlSb.ToString();
        } 
        #endregion



        public string GetRequestParamJsonStr(ApiRequest request)
        {
            var jsonResult= JsonConvert.SerializeObject(request);
            if (jsonResult=="{}")
            {
                return "";
            }
            else
            {
                return jsonResult;
            }
        }
        private string GetAuthorization(string userName, string pwd)
        {
            return SignHelper.Base64EnCode(userName + ":" + pwd);
        }

        public ApiResponse Execute(ApiRequest request)
        {
            return Execute<ApiResponse>(request);
        }

    }
}
